class GameScene extends eui.Component implements eui.UIComponent {
	public constructor(id:number) {
		super();
		this.dataId = id;
	}
	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}
	protected childrenCreated(): void {
		super.childrenCreated();
		this.init();
		this.initPanel();
	}
	private initPanel() {
		this.btn_menu.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			this.group_setting.visible = true;
		}, this)
		this.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			this.group_setting.visible = false;
		}, this)
		this.btn_selectLevel.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			SceneManager.Instance.switchScene(SceneManager.Instance.selectLevel);
		}, this)
		this.btn_xinshou.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			this.group_xinshou.visible = true;
			this.group_setting.visible = false;
			this.group_xinshou.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
				if (this.group_xinshou.visible)
					this.group_xinshou.visible = false;
			}, this);
		}, this)
	}
	public btn_menu: eui.Image;
	public group_setting: eui.Group;
	public btn_close: eui.Image;
	public btn_xinshou: eui.Image;
	public btn_selectLevel: eui.Image;
	public group_xinshou: eui.Group;
	public rect: eui.Rect;
	private line1: egret.Shape = new egret.Shape();
	private line2: egret.Shape = new egret.Shape();
	private line3: egret.Shape = new egret.Shape();
	private timeSpeed: number = 500;
	private userLineNum = 0;
	public img: eui.Image;
	public bg: eui.Rect;
	/**关卡Id */
	private dataId:number;
	private pointArr: [{ x: number, y: number }] = [{ x: 159, y: 550 }, { x: 481, y: 550 }, { x: 468, y: 608 }, { x: 425, y: 657 }, { x: 360, y: 685 }, { x: 280, y: 683 }, { x: 216, y: 660 }, { x: 173, y: 612 }, { x: 161, y: 550 }];
	private index: number = 0;
	private userLine: egret.Bitmap = new egret.Bitmap(RES.getRes("heidian_png"));
	/**线段总长度 */
	private lineSum: number;
	private init() {
		console.log(`第${this.dataId}关`);
		this.userLine.width = 2;
		this.bg.addEventListener(egret.TouchEvent.TOUCH_BEGIN, () => {
			this.bg.addEventListener(egret.Event.ENTER_FRAME, this.userDrawLine, this)
		}, this)
		this.bg.addEventListener(egret.TouchEvent.TOUCH_END, () => {
			this.bg.removeEventListener(egret.Event.ENTER_FRAME, this.userDrawLine, this)
			this.bg.touchEnabled = false;
			this.draw(this.index);
		}, this)
		this.drawDashedLines();
	}
	private userDrawLine() {
		this.userLineNum += 20;
		this.userLine.skewX = -180;
		this.userLine.x = this.pointArr[0].x;
		this.userLine.y = this.pointArr[0].y;
		this.userLine.height = this.userLineNum;
		this.addChildAt(this.userLine, this.numChildren - 3);
	}
	/**旋转之后的结果 */
	private result;
	private draw(i: number) {
		if (i >= this.pointArr.length || this.userLineNum == 0) {
			return
		}
		var newX, newY;
		if (i == 0) {
			newX = this.pointArr[0].x;
			newY = 493 - this.userLineNum;
		} else {
			newX = this.result.newX;
			newY = this.result.newY
		}
		//算出旋转角度值
		if (i > this.pointArr.length - 2) return
		this.result = this.calcAngle(newX, newY, this.pointArr[i].x, this.pointArr[i].y, this.pointArr[i + 1].x, this.pointArr[i + 1].y);
		let ang = this.result.ang;
		egret.Tween.get(this.userLine).to({ rotation: this.userLine.rotation + this.result.ang }, this.timeSpeed).call(() => {
			let len = this.pointArr.length
			let endDistance = egret.Point.distance(new egret.Point(this.pointArr[len - 1].x, this.pointArr[len - 1].y), new egret.Point(this.pointArr[len - 2].x, this.pointArr[len - 2].y))
			if (i >= this.pointArr.length - 2) {
				if (this.userLineNum < endDistance) {
					console.log("没画完");
				} else {
					console.log("超出了");
				}
				return;
			}
			let distance = egret.Point.distance(new egret.Point(this.pointArr[i].x, this.pointArr[i].y), new egret.Point(this.pointArr[i + 1].x, this.pointArr[i + 1].y))
			let line: egret.Shape = new egret.Shape()
			line.graphics.lineStyle(2, 0x000000);
			line.graphics.moveTo(this.pointArr[i].x, this.pointArr[i].y);
			if (this.userLineNum < 0 || this.userLineNum < distance) {
				console.log("线段剩余：", this.userLineNum);
				console.log("完整距离：", distance);
				return;
			}
			line.graphics.lineTo(this.pointArr[i + 1].x, this.pointArr[i + 1].y);
			line.graphics.endFill();
			this.addChildAt(line, this.numChildren - 3);
			this.userLine.height = this.userLineNum = this.userLineNum - distance;
			this.lineSum += distance;
			this.userLine.x = this.pointArr[i + 1].x;
			this.userLine.y = this.pointArr[i + 1].y;
			this.index++;
			this.draw(this.index)
		})
	}
	/**
     * 假设三角形为ABC则计算角B的角度，需按顺序传入A,B,C坐+标点
     */
	private calcAngle(x1, y1, x2, y2, x3, y3): { sin: number, cos: number, newX: number, newY: number, ang: number } {
		// （x1，y1）为要转的点，（x2,y2）为中心点，如果是顺时针角度为θ，
		// x=(x1-x2)cosθ-(y1-y2)sinθ+x2
		// y=(y1-y2)cosθ+(x1-x2)sinθ+y2
		// 已知α是三角形的一个内角。且已求出cosα=a; ∣a∣<1;
		// 当0<a<1时α是锐角；当-1<a<0时α是钝角；
		// 当a=0时α=90°；
		let C = egret.Point.distance(new egret.Point(x1, y1), new egret.Point(x2, y2));
		let A = egret.Point.distance(new egret.Point(x2, y2), new egret.Point(x3, y3));
		let B = egret.Point.distance(new egret.Point(x1, y1), new egret.Point(x3, y3));
		let cosB = (A * A + C * C - B * B) / (2 * A * C)
		let sinB = 1 / cosB;
		// console.log("cosB", cosB);
		if (Math.abs(cosB) >= 1) {
			console.log("计算出错");
			return
		} else if (cosB > 0 && cosB < 1) {
			console.log("角B为锐角");
		} else if (cosB > -1 && cosB < 0) {
			console.log("角B为钝角");
		} else if (cosB == 0) {
			console.log("角B为直角");
		}
		let ang = Math.acos(cosB) * 180 / Math.PI;
		console.log("角度为    ", ang);
		// let hu = 2*Math.PI/(360*ang);
		let sin = Math.sin(ang * Math.PI / 180);
		let cos = Math.cos(ang * Math.PI / 180);
		let x = (x1 - x2) * cos - (y1 - y2) * sin + x2
		let y = (y1 - y2) * cos + (x1 - x2) * sin + y2
		console.log(`旋转完的x,y为：{${x},${y}}`);
		return { sin: sin, cos: cos, newX: x, newY: y, ang: ang };
	}
	private drawDashedLines() {
		// 绘制虚线框
		let shp: egret.Shape = new egret.Shape();
		shp.graphics.lineStyle(2, 0x00ff00, 1, true, "", "", "", 1, [10, 10]);
		shp.graphics.moveTo(this.pointArr[0].x, this.pointArr[0].y);
		for (let i = 1; i < this.pointArr.length; i++) {
			if (i == this.pointArr.length - 3) {
				shp.graphics.lineStyle(2, 0x000000, 1, true, "", "", "", 1, [10, 10]);
			} else if (i == this.pointArr.length - 2) {
				shp.graphics.lineStyle(2, 0x0fff0, 1, true, "", "", "", 1, [10, 10]);

			} else if (i == this.pointArr.length - 1) {
				shp.graphics.lineStyle(2, 0x057400, 1, true, "", "", "", 1, [10, 10]);
			}
			shp.graphics.lineTo(this.pointArr[i].x, this.pointArr[i].y);
		}
		shp.graphics.lineTo(this.pointArr[0].x, this.pointArr[0].y);
		shp.graphics.endFill();
		this.addChildAt(shp, this.numChildren - 3);
	}
}